package com.soft.small.app.entity;

import com.soft.small.app.service.AppHttpRequest;
import com.soft.small.app.service.AppMqttConnect;
import com.soft.small.host.mqtt.HostMqttCallback;
import com.soft.small.app.config.APPConfig;
import com.soft.small.util.http.SmallHttp;
import com.soft.small.util.mqtt.MyMqttClient;
import com.soft.util.encrypt.MD5Utils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;

@Data
@Slf4j
public class AppClient {
    private String mobile;
    private String si;
    private String appSecret;
    private String appKey;
    private String token = null;
    private String userUnique;
    private String username;
    private String password;
    private String clientId;
    private String mqttUrl;
    private String hostUrl;
    private String gatewayUrl;
    private String subTopic;
    private MyMqttClient myMqttClient;
    private SmallHttp smallHttp;
    private String env;
    private MqttConnectOptions options;
    private String sn;

    public AppClient(String env, String mobile, String sn) {
        this.env = env;
        this.mobile = mobile;
        this.sn = sn;
        if (env.toLowerCase().startsWith("chk")) {
            this.appKey = APPConfig.CHK_appKey;
            this.appSecret = APPConfig.CHK_appSecret;
            gatewayUrl = APPConfig.CHK_GatewayUrl;
            if (env.toLowerCase().endsWith("local")) {
                mqttUrl = hostUrl;
            }else {
                mqttUrl = APPConfig.CHK_MqttUrl;
            }
        }else if (env.toLowerCase().startsWith("release")){
            this.appKey = APPConfig.Release_appKey;
            this.appSecret = APPConfig.Release_appSecret;
            gatewayUrl = APPConfig.Release_GatewayUrl;
            if (env.toLowerCase().endsWith("local")) {
                mqttUrl = hostUrl;
            }else {
                mqttUrl = APPConfig.Release_MqttUrl;
            }
        }
        smallHttp = new SmallHttp(appKey,appSecret,gatewayUrl);
    }

    /**
     * 必须获取si然后才能初始化
     */
    public void init(){
        clientId = "uiot_app-" + si;
        subTopic = "app/+/+/" + si + "/+";
        Long time = System.currentTimeMillis();
        if (token != null){
            username = appKey + "|" + si + "|uiot_app|" + time +"|"+ token;
        }else {
            username = appKey + "|" + si + "|uiot_app|" + time;
        }
        password = MD5Utils.MD5Encoding(username + appSecret, "");


    }

    /**
     * 必须初始化后才能获取mqtt连接
     */
    public void setMqttConnect(){
        //log.info("++++++ APP客户端的信息是：{}",this.toString());
        myMqttClient = new MyMqttClient(mqttUrl, clientId, username, password);
        myMqttClient.setAppKey(appKey);
        myMqttClient.setAppSecret(appSecret);
        myMqttClient.setMqttClient(options,new HostMqttCallback(myMqttClient));
        myMqttClient.subscribe(subTopic);
        log.info("++++++ APP的 mqttClient是：{}", myMqttClient.toString());
    }

    public void connect(){
        //注册app,获取si,再app登录获取token
        AppHttpRequest appLogin = new AppHttpRequest(this);
        appLogin.register();
        appLogin.appVerifyCodeLogin();
        appLogin.listHomeSn();
        //获取si/token才能初始化
        init();
        //必须实例化AppMqttConnect
        new AppMqttConnect(this);
        //初始化后,才能进行mqtt连接
        setMqttConnect();
    }
}
